package Listeners;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUI.AuthorToolPanel;
import GUI.ConferenceWizardFrame;
import GUI.CurrentUserPanel;
import GUI.ProgramChairPanel;
import GUI.ReviewerToolPanel;
import GUI.SubChairToolPanel;

import Model.Conference;
import Model.ConferenceManager;
import Model.Conference.Role;

/**
 * Listener used for the "Access Conference" buttons in 
 * ConferenceList Panel and also for the "Change Roles"
 * button in the CurrentUserPanel.
 * 
 * Allows users to choose what role they would like to use
 * while accessing the conference that has been selected.
 * 
 * Enables user to change to a different role even after selecting
 * and initial role to access the conference.
 * 
 * @author Paul Schultz (ps87@uw.edu)
 * @author Nathan Judkins (nate001@.uw.edu)
 * @version 12/01/2012
 */
@SuppressWarnings("serial")
public class AccessConferenceListener extends AbstractAction
{
  private Conference my_conference;
  private ConferenceWizardFrame my_frame;
  private ConferenceManager my_manager;
  
  /**
   * Constructs a new AccessConferenceListener.
   * 
   * @param the_conf the Conference to be accessed.
   * @param the_manager The ConferenceManager containing the conference.
   * @param the_frame The master frame for this application.
   */
  public AccessConferenceListener(final Conference the_conf, 
      final ConferenceManager the_manager, final ConferenceWizardFrame the_frame)
  {
    my_conference = the_conf;
    my_frame = the_frame;
    my_manager = the_manager;
  }

/**
 * Populates a JOptionPane that allows the user
 * to choose their role from available options
 * for the conference that is being accessed.
 * 
 * @author Nathan Judkins (nate001@.uw.edu)
 */
  @Override
  public void actionPerformed(ActionEvent arg0)
  {
    List<Role> currentUserRoles = my_conference.getRoles(my_manager.getCurrentUser());
    
    boolean isAuthor = false;
    
    for (Role r : currentUserRoles)
    {
      if (r == Conference.Role.AUTHOR)
      {
        isAuthor = true;
      }
    }      
    
    if (!isAuthor)
      currentUserRoles.add(Conference.Role.NEW_USER);
    
    Role r = (Role) JOptionPane.showInputDialog(null, "Select a role.",
        "Role", JOptionPane.PLAIN_MESSAGE, null, currentUserRoles.toArray(), Role.AUTHOR);
    
    if (r != null)
    {
         JPanel new_role = new AuthorToolPanel(my_conference, my_manager.getCurrentUser(), 
          my_frame);
      
      // Moves user to proper role Panel depending on what choice they made.
      switch (r)
      {
        case AUTHOR:
          new_role = new AuthorToolPanel(my_conference, my_manager.getCurrentUser(), 
              my_frame);
          break;
        case REVIEWER: 
          new_role = new ReviewerToolPanel(my_conference, my_manager.getCurrentUser(),
              my_frame);
          break;
        case SUB_CHAIR:
          new_role = new SubChairToolPanel(my_conference, my_manager.getCurrentUser(), my_frame);
          break;
        case PROGRAM_CHAIR:
          new_role = new ProgramChairPanel(my_conference,
                                           my_manager.getCurrentUser(), 
                                           my_frame);
          break;
        case NEW_USER:
          new_role = new AuthorToolPanel(my_conference, my_manager.getCurrentUser()
              , my_frame);
          break; 
      }
      
      my_frame.setCurrentPanel(new_role);
      
      my_frame.add(new CurrentUserPanel(my_frame, my_manager.getCurrentUser(),
                                        my_conference, my_manager), BorderLayout.WEST);
      //my_frame.setMinimumSize(new Dimension(1050, 600));
    }
    my_frame.pack();
  }
}
